﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Xml.Linq;
using Drifter.Core;
using Drifter.Tools;
namespace DrifterTest
{
    [TestClass]
    public class LinqToXml_Test1
    {
        [TestMethod]
        public void TestMethod1()
        {
        }

        [TestMethod]
        public void TestMethod2()
        {

            String xml =
              @"<Pricing>
                    <MPrice>
                        <Id>0079</Id>
                        <Price>
                            <Price>31.25</Price>
                            <StartDt>2009-8-01</StartDt>
                            <EndDt>2009-08-26</EndDt>
                        </Price>
                        <Price>
                            <ListPrice>131.25</ListPrice>
                            <StartDt>2009-08-26</StartDt>
                            <EndDt>9999-12-31</EndDt>
                        </Price>
                    </MPrice>
                </Pricing>";

            var root = XElement.Parse(xml).Elements("MPrice").Elements("Price");

            var priceInfo = from e in root
                            let start = DateTime.Parse(e.Descendants("StartDt").FirstOrDefault().Value)
                            let end = DateTime.Parse(e.Descendants("EndDt").FirstOrDefault().Value)
                            where start < DateTime.Now && end > DateTime.Now
                            select new { Id = e.Parent.Element("Id").Value, ListPrice = e.Element("ListPrice").Value };


            string id = priceInfo.FirstOrDefault().Id.ToString();
            Assert.AreEqual("0079", id);

            string listPrice = priceInfo.FirstOrDefault().ListPrice.ToString();
            Assert.AreEqual("131.25", listPrice);

        }


        [TestMethod]
        public void TestMethod3()
        {
            String xml =
             @"<configuration>
                  <fields>
                      <field name='Field1'  source='Main' pos='12345' length='12345' enabled='tRue'/>
                      <field name='Field2'  source='Main'  enabled='True'/>
                      <field name='Flexfield'  source='Main'  flexible='true'/>
                  </fields>
               </configuration>";
    
            IEnumerable<XElement> root = XElement.Parse(xml).Elements("fields").Elements("field");

            var fieldDefs = from e in root
                            select new FieldDef()
                            {
                                Name = e.AttributeValue<string>("name"),
                                Source = e.AttributeValue<string>("source"),
                                Pos = e.AttributeValue<int?>("pos"),
                                Length = e.AttributeValue<int?>("length"),
                                Enabled = e.AttributeValue<bool>("enabled"),
                                Flexible = e.AttributeValue<bool>("flexible")
                            };

            Assert.AreEqual(3, fieldDefs.Count());
            Assert.AreEqual(12345, fieldDefs.ToArray()[0].Pos);
            Assert.AreEqual(true, fieldDefs.ToArray()[0].Enabled);
            Assert.AreEqual(null, fieldDefs.ToArray()[1].Pos);
            Assert.AreEqual(true, fieldDefs.ToArray()[2].Flexible);
            
        }



       








    }
}


